//
//  Set_login_pwd_vc.m
//  ainkjj
//
//  Created by zhiyiLiu on 2020/3/3.
//  Copyright © 2020 zhiyiLiu. All rights reserved.
//

#import "Set_login_pwd_vc.h"

@interface Set_login_pwd_vc ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *code_one_tf;
@property (weak, nonatomic) IBOutlet UITextField *code_two_tf;

@end

@implementation Set_login_pwd_vc
- (IBAction)code_two_tf_see_action:(UIButton *)sender {
    sender.selected = !sender.selected;
    self.code_two_tf.secureTextEntry = !self.code_two_tf.secureTextEntry;

}
- (IBAction)code_one_tf_see_action:(UIButton *)sender {
    sender.selected = !sender.selected;
    self.code_one_tf.secureTextEntry = !self.code_one_tf.secureTextEntry;
}
- (IBAction)confirm_action:(UIButton *)sender {
        
    
    if(self.code_one_tf.text.length < 6 || self.code_one_tf.text.length > 16 || self.code_two_tf.text.length < 6 || self.code_two_tf.text.length > 16){
        [MBProgressHUD showTipMessageInView:@"请输入6-16位字母数字组合"];
        return;
    }

    if (![self.code_one_tf.text isEqualToString:self.code_two_tf.text]) {
        [MBProgressHUD showTipMessageInView:@"密码不一致"];
        return;
    }
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:[Anktool strBlankHolder:self.phoneNum] forKey:@"mobile"];
    [dict setObject:[Anktool strBlankHolder:self.verification] forKey:@"verification"];
    [dict setObject:app_code forKey:@"company"];
    [dict setObject:app_code forKey:@"app_code"];

    [dict setObject:self.code_one_tf.text forKey:@"password"];

    
    sender.userInteractionEnabled = NO;
    [PPHTTPRequest req:@"api/Auth/signup" parameters:dict success:^(id response) {
        NSString *error_code = [NSString stringWithFormat:@"%@", response[@"error_code"]];
        if ([error_code isEqualToString:@"0"]) {
            [MBProgressHUD showTipMessageInWindow:@"注册完成，请登录"];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
        else  {
            
            [MBProgressHUD showTipMessageInView:response[@"error_msg"]];
            
        }
        sender.userInteractionEnabled = YES;
    } failure:^(NSError *error) {
        [MBProgressHUD showTipMessageInView:[error localizedDescription]];
        sender.userInteractionEnabled = YES;
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
//    [self.code_one_tf becomeFirstResponder];
    
    // /api/Auth/signup
    // password=123456&mobile=18162152664&company=ainankang&verification=395417&app_code=ainankang
    /*
     {
     "error_code":0,
     "error_msg":"操作成功",
     "data":{
     "user_info":{
     "id":84,
     "user_name":"18162152664",
     "avatar_url":"",
     "gender":0,
     "mobile":"18162152664",
     "status":1
     },
     "session_id":"58d792ef-5cfe-11ea-acc6-00163e0b4265",
     "token":"wq56wqbCrcKxwqp6w519wqV0w5t9fMKXZsKBwrJ5acKCwojCksOcwrLCucKIwq_Ch8ONwpLCkcKqwrDCumrCvsKqwpzCm33DjsKJw5F8wozChsKgwpLCpcKJwqnCgsKIf8OPwr7CuGTCnsKSwpXChsKTwq3CisKmwq3CscK6wpLDoMKKwrV1w41-fHlowobCiXFy"
     }
     }
     */
    self.code_one_tf.delegate = self.code_two_tf.delegate = self;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - ================== tf delegate ==================
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    NSMutableString *newtxt = [NSMutableString stringWithString:textField.text];
    
    [newtxt replaceCharactersInRange:range withString:string];
    
    if ([textField isEqual:self.code_one_tf] || [textField isEqual:self.code_two_tf]) {
        int MAX_CHARS = 16;
        return ([newtxt length] <= MAX_CHARS && [Anktool is_num_alpha:newtxt]);
        
    }
    
    return YES;
    
}
@end
